/*
 * 
 * Copyright 2010 David H Pollack
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * 
 */

package fantasy;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;

import net.oauth.OAuthException;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.simpleyql.Api;
import com.simpleyql.ApiFactory;

@SuppressWarnings("serial")
public class FantasyServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        /*
        // Set to expire far in the past.
		resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
		// Set standard HTTP/1.1 no-cache headers.
		resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
		resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
		// Set standard HTTP/1.0 no-cache header.
		resp.setHeader("Pragma", "no-cache");
		*/
        
        Properties props = new Properties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("fantasy.properties"));
        
        String baseurl = props.getProperty("application.baseurl");
        
        if (user != null) {

    		PersistenceManager pm = PMF.get().getPersistenceManager();
            
    	    Query query = pm.newQuery(UserTable.class);
    	    query.setFilter("userkey == nickNameParam");
    	    query.declareParameters("String nickNameParam");
    	    
    	    try {
    	    	List<UserTable> uquery = (List<UserTable>) query.execute(user.getNickname());
    		    if (uquery.isEmpty()) {
    	        	Api api = ApiFactory.getApiInstance(
    	    	            "dj0yJmk9NGVDelJyN2MzWW5zJmQ9WVdrOVRFcGFaalZrTkdrbWNHbzlNak16TVRFNE5qWXkmcz1jb25zdW1lcnNlY3JldCZ4PTM2",
    	    	            "91b08a397dc663f4372f6a4fa31e693f6ab7bf54",
    	    	            baseurl + "/simpleyqlcallback/",
    	    	            false, null);

    	    	    
    	    	    try {
    	    			api.askAuthorization(req, resp, baseurl + "/return");
    	    		} catch (OAuthException e) {
    	    			// TODO Auto-generated catch block
    	    			e.printStackTrace();
    	    		}
    		    } else {
    		    	resp.sendRedirect(baseurl + "/return");
    		    }
    	    } finally {
    	        query.closeAll();
    	    }
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }


	    
	    
        //resp.setContentType("text/plain");

		
		
		/*		
		// Get my yahoo GUID
		//http://social.yahooapis.com/v1/me/guid
		 */	
	}
}
